package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.common.IpToAddressUtil;
import com.api.shopapi.common.IpUtil;
import com.api.shopapi.common.RedisHashUtil;
import com.api.shopapi.entity.Elderly;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.User;
import com.api.shopapi.service.ElderlyService;
import com.api.shopapi.service.UserService;
import com.api.shopapi.util.smsTencent.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("Test")
public class TestController extends BaseController{

    //从配置文件中读取实例名称
    @Value("${eureka.instance.instance-id}")
    String instanceID;


    @Autowired
    RedisHashUtil<String> redisHashUtil;


    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;


    //GetRedisKeys
    @RequestMapping(value = "GetRedisKeys", method = RequestMethod.GET)
    public MyResult GetRedisKeys() {
        try {



            Set<String> set = redisHashUtil.getKeys();



            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("set",set);


//            String pretty = JSON.toJSONString(jsonObject1, SerializerFeature.PrettyFormat,
//                    SerializerFeature.WriteMapNullValue,
//                    SerializerFeature.WriteDateUseDateFormat);


            return success(jsonObject1);
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }



    @RequestMapping(value = "Get",method = RequestMethod.GET)
    public String Get() {

        //SendSms sendSms = new SendSms();
        //sendSms.Send("18019909040");

        System.out.println("--get----from---"+instanceID+"----------");
        return "--get----from---"+instanceID+"----------";
    }


    @RequestMapping(value = "SetRedis",method = RequestMethod.GET)
    public String SetRedis() {
        redisHashUtil.put("key","value",-1);
        return "set redis success";
    }

    @RequestMapping(value = "GetRedis",method = RequestMethod.GET)
    public String GetRedis() {
        String value = redisHashUtil.get("key");
        return value;
    }


    @RequestMapping(value = "GetAddress",method = RequestMethod.GET)
    public String GetAddress(){
        String address = IpToAddressUtil.getCityInfo("36.57.163.183");
        return address;
    }

    @RequestMapping(value = "GetIp", method = RequestMethod.GET)
    public String GetIp(HttpServletRequest request) {
        return IpUtil.getIpAddr(request);
    }

    @RequestMapping(value = "GetOutIp", method = RequestMethod.GET)
    public String GetOutIp(){
        return IpUtil.getV4IP();
    }



    @RequestMapping(value = "updateLoginName", method = RequestMethod.GET)
    public String updateLoginName(){

        List<User> listUser = userService.getListAll();
        for(User user:listUser){

            if(user.getUserType() == 1){
                Elderly elderly = elderlyService.getByUserCode(user.getUserCode());
                user.setLoginName(elderly.getIdCard());
                userService.update(user);
                System.out.println(elderly.getIdCard()+"---");
            }

        }

        return "over";
    }




}